package com.example.springcloud.controller;

import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.example.springcloud.request.PublishRequest;
import com.example.springcloud.vo.PostsVo;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableSupport;
import io.swagger.annotations.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.example.springcloud.domain.Posts;
import com.example.springcloud.service.IPostsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户种草内容Controller
 * 
 * @author hx
 * @date 2025-02-14
 */
@RestController
@RequestMapping("/group_buy/posts")
@Api(tags = "用户种草文章管理")
public class PostsController extends BaseController
{
    @Autowired
    private IPostsService postsService;

    /**
     * 查询用户种草内容列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询用户种草内容列表")
    @ApiResponses(value = { @ApiResponse(code = 200, message = "查询成功", response = PostsVo.class, responseContainer = "List") })
    @Anonymous
    public TableDataInfo list(Posts posts)
    {
        // 处理分页参数
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        List<PostsVo> list = postsService.selectPostsList(posts);
        //处理上面查询的list集合
        int num = list.size();
        list = list.stream().skip((pageNum - 1) * pageSize).limit(pageSize).collect(Collectors.toList());
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setRows(list);
        rspData.setTotal(num);
        return rspData;
    }


    /**
     * 获取用户种草内容详细信息
     */
    @GetMapping(value = "/{postId}")
    @ApiOperation(value = "获取用户种草内容详细信息")
    @ApiImplicitParam(name = "postId", value = "用户种草内容ID", required = true, dataType = "Long", paramType = "path")
    @ApiResponses(value = { @ApiResponse(code = 200, message = "查询成功", response = PostsVo.class) })
    @Anonymous
    public AjaxResult getInfo(@PathVariable("postId") Long postId)
    {
        return success(postsService.selectPostsByPostId(postId));
    }

    /**
     * 新增用户种草内容
     */
    @PreAuthorize("@ss.hasPermi('group_buy:posts:add')")
    @Log(title = "用户种草内容", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Posts posts)
    {
        return toAjax(postsService.insertPosts(posts));
    }

    /**
     * 修改用户种草内容
     */
    @PreAuthorize("@ss.hasPermi('group_buy:posts:edit')")
    @Log(title = "用户种草内容", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Posts posts)
    {
        return toAjax(postsService.updatePosts(posts));
    }

    /**
     * 删除用户种草内容
     */
    @PreAuthorize("@ss.hasPermi('group_buy:posts:remove')")
    @Log(title = "用户种草内容", businessType = BusinessType.DELETE)
	@DeleteMapping("/{postIds}")
    public AjaxResult remove(@PathVariable Long[] postIds)
    {
        return toAjax(postsService.deletePostsByPostIds(postIds));
    }

    /**
     * 用户发表种草文章
     */
    @PostMapping("/publishPosts")
    @ApiOperation(value = "用户发表种草文章")
    @ApiImplicitParam(name = "publishRequest", value = "用户发表种草文章", required = true, dataType = "publishRequest", paramType = "body")
    @ApiResponses(value = { @ApiResponse(code = 200, message = "发布成功")})
    @Anonymous
    public AjaxResult publishPosts(@RequestBody PublishRequest publishRequest)
    {
        return postsService.publishPosts(publishRequest);
    }
}
